package com.zbo.util;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class SqlSessionFactoryUtils {
    private static final SqlSessionFactory sqlSessionFactory;

    static{
        //随着类加载执行且只执行一次
        String name = "mybatis-config.xml";
        InputStream inputStream = SqlSessionFactoryUtils.class.getClassLoader().getResourceAsStream(name);

        if (inputStream == null) {
            throw new RuntimeException("mybatis-config.xml not found in classpath!");
        }else{
            System.out.println("success in loading mybatis-config.xml!");
        }

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
}
